package gestionBulletins.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import gestionBulletins.controller.SectionsTableModel;
import gestionBulletins.model.Section;
import gestionBulletins.model.persistance.DaoFactory;
import gestionBulletins.model.persistance.DaoSection;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class InfosSectionWindow extends JFrame implements ActionListener, ListSelectionListener {

	private static final long serialVersionUID = 1L;
	private MainWindow mw;
	private DaoFactory daoFactory;
	public JTable table;
	public SectionsTableModel tableModel;
	private JButton boutonAjouter;
	private JButton boutonModifier;
	private JButton boutonEnlever;
	private JButton boutonFermer;

	public InfosSectionWindow(DaoFactory daoFactory, MainWindow mw) {
		super();
		this.daoFactory = daoFactory;
		this.mw = mw;
		setResizable(false);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setTitle("Liste des sections");
		initComposantsGraphiques();
		setupFrame();
		pack();
	}
	
	private void initComposantsGraphiques() {
		tableModel = new SectionsTableModel(daoFactory);
		table = new JTable(tableModel);
		table.getSelectionModel().addListSelectionListener(this);
		boutonAjouter = new JButton("Ajouter");
		boutonModifier = new JButton("Modifier");
		boutonEnlever = new JButton("Enlever");
		boutonFermer = new JButton("Fermer");
		boutonAjouter.addActionListener(this);
		boutonModifier.addActionListener(this);
		boutonEnlever.addActionListener(this);
		boutonFermer.addActionListener(this);
		boutonModifier.setEnabled(false);
		boutonEnlever.setEnabled(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
	
	private void setupFrame() {
		Container pane = getContentPane();
		BorderLayout bl = new BorderLayout(10,10);
		pane.setLayout(bl);
		JScrollPane sp = new JScrollPane(table);
		getRootPane().setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
		
		/* haut de page */
		JLabel sousTitre = new JLabel("Liste des différentes sections, représentées par leur président");
		sousTitre.setAlignmentX(CENTER_ALIGNMENT);
		pane.add(sousTitre, BorderLayout.NORTH);
		
		/* table au milieu */
		pane.add(sp, BorderLayout.CENTER);
		
		/* bas de page */
		JPanel basDePage = new JPanel();
		FlowLayout fl = new FlowLayout();
		basDePage.setLayout(fl);
		basDePage.add(boutonAjouter);
		basDePage.add(boutonModifier);
		basDePage.add(boutonEnlever);
		basDePage.add(Box.createRigidArea(new Dimension(25,0)));
		basDePage.add(boutonFermer);
		pane.add(basDePage, BorderLayout.SOUTH);
	}
	
	public void repaint() {
		//System.out.println("on me repaint !");
		tableModel.update(null, null);
		table.revalidate();
		table.repaint();
		/* on recupere la liste des sections a jour */
		DaoSection ds = daoFactory.getDaoSection();
		Collection<String> sections = ds.getAllPresident();
		String[] enumSections = new String[sections.size() + 1];
		int cpt = 0;
		enumSections[cpt++] = "dans aucune section";
		for (String president : sections) {
			//System.out.println("president de section: " + president);
			enumSections[cpt++] = president;
		}
		for (String string : enumSections) {
			//System.out.println("debug:" + string);
		}
		/* on raffraichit la combobox dans insertwindow */
		mw.insertWindow.inputSections.setModel(new DefaultComboBoxModel(enumSections));
		/* on raffraichit la combobox dans infosadherentWindow */
		mw.consultWindow.infosAdherentWindow.inputSections.setModel(new DefaultComboBoxModel(enumSections));
		super.repaint();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == boutonFermer) {
			setVisible(false);
		}
		if (e.getSource() == boutonAjouter) {
			AddSectionWindow aew = new AddSectionWindow(daoFactory, this);
			aew.setVisible(true);
		}
		
		if (e.getSource() == boutonModifier) {
			int numLigne = table.convertRowIndexToModel(table.getSelectedRow());
			Section section = tableModel.getSection(numLigne);
			ModifSectionWindow mew = new ModifSectionWindow(daoFactory, this, section);
			mew.setTitle("Modifications d'un enfant");
			mew.setVisible(true);
		}
		
		if (e.getSource() == boutonEnlever) {
			int confirmerSuppression = JOptionPane.showConfirmDialog(this,
					"Voulez vous vraiment supprimer cette section ?",
					"Suppression",
					JOptionPane.YES_NO_OPTION);
			if (confirmerSuppression == 0) {
				DaoSection ds = daoFactory.getDaoSection();
				int numLigne = table.convertRowIndexToModel(table.getSelectedRow());
				//System.out.println("numero de ligne:" + numLigne);
				Section section = tableModel.getSection(numLigne);
				ds.delete(section);
				tableModel.removeRow(numLigne);
				repaint();
			}
		}
		
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting())
            return;
		/* si y'a pas de selection d'un enfant on desactive le bouton effacer et modifier */
		if (((ListSelectionModel)e.getSource()).isSelectionEmpty()) {
			boutonModifier.setEnabled(false);
			boutonEnlever.setEnabled(false);
		}
		/* sinon c'est bon */
		else {
			boutonModifier.setEnabled(true);
			boutonEnlever.setEnabled(true);
		}
	}
}
